package com.cms.manage.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.cms.manage.common.SysCode;
import com.cms.manage.model.CmsStoreList;
import com.cms.manage.service.ClassfyService;
import com.cms.manage.service.StoreListService;
import com.cms.manage.service.StoreLogService;
import com.cms.manage.util.BeanUtil;
import com.cms.manage.util.DateUtil;
import com.cms.manage.util.ExcelExportUtil;
import com.cms.manage.util.ListUtil;
import com.cms.manage.util.RequestMapToUtil;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
@Api(description = "库存管理")
//@ApiIgnore()
@RestController     //等同于同时加上了@Controller和@ResponseBody
@RequestMapping("/cms/store")
public class StoreController extends BaseController{

      private static final Logger Log = LoggerFactory.getLogger(StoreController.class);
  
	  @Autowired
	  private HttpSession session;  
	  @Autowired
	  private ClassfyService classfyService;
	  @Autowired
	  private StoreListService storeListService;
	  @Autowired
	  private StoreLogService storeLogService;
	 
	  
	    @ApiOperation(value="添加进货单", notes="添加进货单")
		@ApiImplicitParams({
		@ApiImplicitParam(name = "bankName", value = "供应商", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "goodName", value = "品名", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "goodSize", value = "规格", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "classfy", value = "分类", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "number", value = "数量", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "area", value = "面积", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "rank", value = "等级说明", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "date", value = "日期", required = false, dataType = "String",paramType = "form"),		
		})
	    @RequestMapping(value = "/saveList",method = RequestMethod.POST)
	    @ResponseBody
	    public ResponseEntity<Map<String,Object>> list (HttpServletRequest request){		
			  Map<String,String> data =RequestMapToUtil.getPostByTextPlain(request);			   
	    	  Map<String, Object> result = new HashMap<String, Object>();
	    	  System.out.println("添加入库单！");
	    	  result = storeListService.saveList(data);
	    	if(result.get("code")==SysCode.error){
	    		return ResponseEntity.status(HttpStatus.OK).body(ERROR(result.get("message"),null));
	    	 }
	    	return ResponseEntity.status(HttpStatus.OK).body(SUCCESS(result.get("message"),null)); 
	    }
	  
	    @ApiOperation(value="获取进货单列表", notes="获取进货单列表")
		@ApiImplicitParams({
		@ApiImplicitParam(name = "bankName", value = "供应商", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "goodName", value = "品名", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "goodSize", value = "规格", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "classfy", value = "分类", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "number", value = "数量", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "area", value = "面积", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "rank", value = "等级说明", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "date", value = "日期", required = false, dataType = "String",paramType = "form"),		
		})
	    @RequestMapping(value = "/searchStoreList",method = RequestMethod.POST)
	    @ResponseBody
	    public ResponseEntity<Map<String,Object>> searchStoreList (HttpServletRequest request){		
			  Map<String,String> data =RequestMapToUtil.getPostByTextPlain(request);			   
	    	  Map<String, Object> result = new HashMap<String, Object>();
	    	  System.out.println("获取进货单列表！");
	    	  result = storeListService.searchStoreList(data);
	    	if(result.get("code")==SysCode.error){
	    		return ResponseEntity.status(HttpStatus.OK).body(ERROR(result.get("message"),null));
	    	 }
	    	return ResponseEntity.status(HttpStatus.OK).body(SUCCESS(result.get("message"),result.get("data"))); 
	    }
	    
	    @ApiOperation(value="下载进货单列表", notes="下载进货单列表")
	  		@ApiImplicitParams({
	  		@ApiImplicitParam(name = "bankName", value = "供应商", required = false, dataType = "String",paramType = "form"),
	  		@ApiImplicitParam(name = "goodName", value = "品名", required = false, dataType = "String",paramType = "form"),
	  		@ApiImplicitParam(name = "goodSize", value = "规格", required = false, dataType = "String",paramType = "form"),
	  		@ApiImplicitParam(name = "classfy", value = "分类", required = false, dataType = "String",paramType = "form"),
	  		@ApiImplicitParam(name = "number", value = "数量", required = false, dataType = "String",paramType = "form"),
	  		@ApiImplicitParam(name = "area", value = "面积", required = false, dataType = "String",paramType = "form"),
	  		@ApiImplicitParam(name = "rank", value = "等级说明", required = false, dataType = "String",paramType = "form"),
	  		@ApiImplicitParam(name = "date", value = "日期", required = false, dataType = "String",paramType = "form"),		
	  		})
	  	    @RequestMapping(value = "/download",method = RequestMethod.POST)
	  	    @ResponseBody
	  	    public void download (HttpServletRequest request,HttpServletResponse response) throws Exception{		
	    	  Map<String, String> data = RequestMapToUtil.getPostByTextPlain(request);
	    	  System.out.println("下载进货单列表！");
	    	  Map<String, Object> result = new HashMap<String, Object>();
	          data.put("pageNum", "1");
	          data.put("pageSize", String.valueOf(Integer.MAX_VALUE));
	          result = storeListService.searchStoreList(data);	    
	          PageInfo<CmsStoreList> pageInfo = (PageInfo<CmsStoreList>) result.get("data");
	          List<CmsStoreList> list = pageInfo.getList();
	          List<Map<String, Object>> datas = BeanUtil.listObjectToListMap(list);
	          List<String> columnNames = ListUtil.toList("库存编号，品名，供应商，规格，分类，库存/张，面积/㎡，等级（说明），入库时间，最近操作时间，操作人");
	          List<String> columnKeys = ListUtil.toList("id,goodName,bankName,goodSize,classfy,number,area,rank,date,updateTime,userName");
	          Map<String, Object> export = ExcelExportUtil.export("库存信息", columnNames, columnKeys,datas);
	          HSSFWorkbook workbook = (HSSFWorkbook) export.get("workbook");
	          String filename = "cmsStore_" + DateUtil.getDateYMDHMS(new Date()) + ".xls";
	          response.reset();
	          response.setContentType("application/vnd.ms-excel;charset=utf-8");
	          response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes(), "UTF-8"));
	          workbook.write(response.getOutputStream());
	  	    }
	        
	    @ApiOperation(value="删除进货单", notes="删除进货单")
		@ApiImplicitParams({
		@ApiImplicitParam(name = "id", value = "入库单Id", required = false, dataType = "String",paramType = "form"),		
		})
	    @RequestMapping(value = "/delete",method = RequestMethod.POST)
	    @ResponseBody
	    public ResponseEntity<Map<String,Object>> delete (HttpServletRequest request){		
			  Map<String,String> data =RequestMapToUtil.getPostByTextPlain(request);			   
	    	  Map<String, Object> result = new HashMap<String, Object>();
	    	  System.out.println("删除进货单！暂未开放");
	    	  //result = storeListService.searchStoreList(data);
	    	if(result.get("code")==SysCode.error){
	    		return ResponseEntity.status(HttpStatus.OK).body(ERROR(result.get("message"),null));
	    	 }
	    	return ResponseEntity.status(HttpStatus.OK).body(SUCCESS(result.get("message"),result.get("data"))); 
	    }
	    @ApiOperation(value="获取分类列表", notes="获取分类列表")
	  		@ApiImplicitParams({
	  	})
	  	    @RequestMapping(value = "/getClassfys",method = RequestMethod.POST)
	  	    @ResponseBody
	  	    public ResponseEntity<Map<String,Object>> getClassfys (HttpServletRequest request){		
	  			  Map<String,String> data =RequestMapToUtil.getPostByTextPlain(request);			   
	  	    	  Map<String, Object> result = new HashMap<String, Object>();
	  	    	  System.out.println("获取分类列表！");	  	    	  
	  	    	  result= classfyService.getClassfys(data);   	  
	  	    	if(result.get("code")==SysCode.error){
	  	    		return ResponseEntity.status(HttpStatus.OK).body(ERROR(result.get("message"),null));
	  	    	 }
	  	    	return ResponseEntity.status(HttpStatus.OK).body(SUCCESS(result.get("message"),result.get("data"))); 
	  	    }
	  
	    @ApiOperation(value="查询入库/出库列表", notes="查询入库/出库列表")
		@ApiImplicitParams({
		@ApiImplicitParam(name = "operate", value = "出入库标识", required = false, dataType = "String",paramType = "form")
		})
	    @RequestMapping(value = "/searchStoreOutAndIn",method = RequestMethod.POST)
	    @ResponseBody
	    public ResponseEntity<Map<String,Object>> searchStoreOutAndIn (HttpServletRequest request){		
			  Map<String,String> data =RequestMapToUtil.getPostByTextPlain(request);			   
	    	  Map<String, Object> result = new HashMap<String, Object>();
	    	  System.out.println("查询入库/出库列表！");
	    	  result = storeLogService.searchStoreOutAndIn(data);
	    	if(result.get("code")==SysCode.error){
	    		return ResponseEntity.status(HttpStatus.OK).body(ERROR(result.get("message"),null));
	    	 }
	    	return ResponseEntity.status(HttpStatus.OK).body(SUCCESS(result.get("message"),result.get("data"))); 
	    }
	    
	    @ApiOperation(value="新增或编辑入库/出库单", notes="新增或编辑入库/出库单")
		@ApiImplicitParams({
		@ApiImplicitParam(name = "operate", value = "出入库标识", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "goodName", value = "品名", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "id", value = "主键", required = false, dataType = "String",paramType = "form")
		})
	    @RequestMapping(value = "/addStoreOutAndInList",method = RequestMethod.POST)
	    @ResponseBody
	    public ResponseEntity<Map<String,Object>> addStoreOutAndInList (HttpServletRequest request){		
			  Map<String,String> data =RequestMapToUtil.getPostByTextPlain(request);			   
	    	  Map<String, Object> result = new HashMap<String, Object>();
	    	  System.out.println("新增或编辑入库/出库单！");
	    	  result = storeLogService.addStoreOutAndInList(data);
	    	if(result.get("code")==SysCode.error){
	    		return ResponseEntity.status(HttpStatus.OK).body(ERROR(result.get("message"),null));
	    	 }
	    	return ResponseEntity.status(HttpStatus.OK).body(SUCCESS(result.get("message"),result.get("data"))); 
	    }
	  
	    @ApiOperation(value="删除入库/出库单", notes="新增或编辑入库/出库单")
		@ApiImplicitParams({
		@ApiImplicitParam(name = "operate", value = "操作标识", required = false, dataType = "String",paramType = "form"),
		@ApiImplicitParam(name = "id", value = "主键", required = false, dataType = "String",paramType = "form"),
		})
	    @RequestMapping(value = "/deleteStoreOutAndInList",method = RequestMethod.POST)
	    @ResponseBody
	    public ResponseEntity<Map<String,Object>> deleteStoreOutAndInList (HttpServletRequest request){		
			  Map<String,String> data =RequestMapToUtil.getPostByTextPlain(request);			   
	    	  Map<String, Object> result = new HashMap<String, Object>();
	    	  System.out.println("删除入库/出库单！");
	    	  result = storeLogService.deleteStoreOutAndInList(data);
	    	if(result.get("code")==SysCode.error){
	    		return ResponseEntity.status(HttpStatus.OK).body(ERROR(result.get("message"),null));
	    	 }
	    	return ResponseEntity.status(HttpStatus.OK).body(SUCCESS(result.get("message"),null)); 
	    }
		
}
